<template>
  <div class="upload">
    <el-upload ref="upload1" style="display: inline-block;" multiple list-type="picture-card" action="" :limit="1" :http-request="httpRequest"
               :on-remove="handleRemove" :on-change="onChange" :before-upload="beforeAvatarUpload"
               :on-success="handleUploadSuccess" :file-list="fileList">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>
    <el-upload
      class="upload-demo"
      multiple list-type="picture-card"
      ref="upload"
      action="https://jsonplaceholder.typicode.com/posts/"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :auto-upload="true">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
      <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>

  </div>
</template>

<script>
import {client, headers } from '@/model/oss'

export default {
  data(){
    return {
      fileList: [],
      url: ''
    }
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit();
    },
    //文件状态改变时的钩子，添加文件、上传成功和上传失败时都会被调用
    onChange(file, fileList) {
      console.log(file, 'file', fileList, 'Filest');
      // this.fileList = fileList
    },
    //文件上传成功时的钩子
    handleUploadSuccess(response, file, fileList) {
      console.log(response, file, fileList, 'response, file, fileList')
    },
    handlePreview(file) {
      console.log(file);
    },
    //文件列表移除文件时的钩子
    handleRemove(e, fileList) {
      // this.fileList = fileList;
    },
    //上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传。
    beforeAvatarUpload(file) {
      // 文件大小校验
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error('文件大小不能超过 10M !');
      }
      return isLt2M;
    },
    async handleUpload (op) {
      console.log('op', op)
      let res = await File.uploadToken(op.file, (res) => op.onProgress(res))
      let temp={
        name:'name',
        url:''
      }
      op.onSuccess(temp)

    },
    //覆盖默认的上传行为，可以自定义上传的实现
    httpRequest(file) {//阿里云OSS上传
      console.log(file)
      //判断扩展名
      const tmpcnt = file.file.name.lastIndexOf(".") // 获取.的下标
      const uploadFileName = file.file.name.substring(0, tmpcnt) // 获取文件名
      console.log(uploadFileName, '文件名——uploadFileName');
      const exname = file.file.name.substring(tmpcnt + 1) // 获取后缀名
      console.log(exname, '后缀名');
      const names = ['jpg', 'jpeg', 'webp', 'png', 'bmp', 'exe', 'yml']
      if (names.indexOf(exname) < 0) {
        this.$message.error("不支持的格式!")
        return false
      }
      const fileName = uploadFileName
      console.log(fileName, '文件名');
      client.put(
        fileName,
        file.file,
        {
          headers
        },
      ).then(res => {
        console.log(res, 'res---header ');
        // this.handleUploadSuccess()
        //   if (res.url) {
        //     this.url.push(res.url)
        //   } else {
        //     this.$message.error('文件上传失败')
        //   }
      }).catch(err => {
        console.log(err, 'err');
      })
    }
  }
}
</script>
